package com.rjj.wlqq.listen.handle.annotation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 *
 * 使用WebFilter注解实现
 *  @WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器
 *  属性filterName声明过滤器的名称,可选
 *  属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
 *
 *  需要开启springboot启动类扫描servlet组件，否则是不生效的，扫描的注解是@ServletComponentScan。
  */
@WebFilter(filterName = "myWebFilter", urlPatterns = "/*")
@Order(2)
public class MyWebFilter implements Filter {
    Logger log = LoggerFactory.getLogger(MyWebFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("WebFilter init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.info("WebFilter doFilter url " + ((HttpServletRequest)request).getRequestURL());
        chain.doFilter(request,response );
    }

    @Override
    public void destroy() {
        log.info("WebFilter destory");
    }
}
